<?php

/**
 * 无锡万科抽奖程序
 * @desc
 */
class Api_Lottery extends PhalApi_Api {

    private $total = 8000;
    private $perDay = 6000;
    private $userTotal = 5;
    private $userPerDay = 1;


    public function getRules() {
        return [
            'extraction' => [
                'openid' => ['name' => 'openid', 'type' => 'string', 'require' => true, 'desc' => '微信openid'],
                'scores' => ['name' => 'scores', 'type' => 'int', 'min' => 0, 'require' => true, 'desc' => '用户积分'],
            ],
            'updteInfo'  => [
                'openid' => ['name' => 'openid', 'type' => 'string', 'require' => true, 'desc' => '微信openid'],
                'name'   => ['name' => 'name', 'type' => 'string', 'require' => true, 'desc' => '名字'],
                'tel'    => ['name' => 'tel', 'type' => 'mobile', 'require' => true, 'desc' => '电话'],
            ],
            'ranking'    => [
                'top' => ['name' => 'top', 'type' => 'int', 'default' => 10, 'max' => 120, 'min' => 1, 'require' => false, 'desc' => '需要获取的数量'],
            ]
//
        ];
    }

    /**
     * 抽奖
     * @desc
     * @return boolean isLottery 是否中奖
     * @return string msg 描述
     */
    public function extraction() {
//        抽奖步骤
//        验证用户

        if ($this->openid=="") {
            throw new PhalApi_Exception("未中奖", 4000);
        };
        $Domain = new Domain_Lottery();
        $data = $Domain->insertUser($this->openid, $this->scores);

        if ($this->scores < 10) {
            throw new PhalApi_Exception("未中奖", 4001);
        }




//        $total = $Domain->gettotalUser($this->openid);
////        if ($total >= $this->perDay) {
////            throw new PhalApi_Exception("未中奖", 4002);
////        }
//
//        return $total;
//        return     $Domain->gettotalUserByTodayy($this->openid);


        $currScores = DI()->cache->zScore("wxvk:ranking",$this->openid);
        if($this->scores>$currScores)DI()->cache->zAdd("wxvk:ranking", $this->scores, $this->openid);

        if (!$data) {
            throw new PhalApi_Exception("未中奖", 4002);
        }

        //        验证总奖品数量
        $total = $Domain->gettotal();
        if ($total >= $this->total) {
            throw new PhalApi_Exception("未中奖", 4003);
        }

//        验证今日奖品数量

        $total = $Domain->gettotalByToday();
        if ($total >= $this->perDay) {
            throw new PhalApi_Exception("未中奖", 4004);
        }

//        验证用户总中奖次数
        $total = $Domain->gettotalUser($this->openid);
        if ($total >= $this->userTotal) {
            throw new PhalApi_Exception("未中奖", 4005);
        }

//        验证用户今日中奖次数
        $total = $Domain->gettotalUserByTodayy($this->openid);
        if ($total >= $this->userPerDay) {
//            $Domain->db->transaction='ROLLBACK';
            throw new PhalApi_Exception("未中奖", 4006);
        }
        if (!$Domain->setLottery($data['id'])) {
            throw new PhalApi_Exception("未中奖", 4007);
        }

        return [
            'isLottery' => true
        ];

    }

    /**
     * 更新用户资料
     * @desc
     * @return boolean isUpdated 是否更新成功
     */
    function updteInfo() {

        //        验证用户
//        if (!DI()->wechat->getUserInfo($this->openid)) {
//            throw new PhalApi_Exception("非法用户", 4000);
//        };


        $Domain = new Domain_Lottery();
        //判断中奖
        $total = $Domain->gettotalUser($this->openid);
        if ($total >= $this->userTotal) {
            throw new PhalApi_Exception("未中奖用户不能登记信息", 4001);
        }
//        设置信息
        $lotteryUsers = new Domain_LotteryUsers();

        return [
            'isUpdated' => $lotteryUsers->update(
                $this->openid,
                $this->name,
                $this->tel
            ) ? true : false

        ];
    }

    /**
     * 排行榜
     * @desc
     *
     * @return string name 用户名称
     * @return int scores 用户积分
     * @return int ranking 用户排名
     *
     */
    function ranking() {
        $Domain = new Domain_Lottery();

        return $Domain->ranking($this->top);
    }

    /**
     * 强制更新排行榜
     * @desc  排行基于缓存，持接口用于强制更新排行榜
     *
     */
    function rankingUpdate() {
        $Domain = new Domain_Lottery();

        return $Domain->rankingUpdate();
    }
}